package com.prd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.prd.dto.OaPositionDTO;
import com.prd.model.OaDeptDO;
import com.prd.model.OaPositionDO;
import com.prd.model.OaPostDO;
import com.prd.service.OaDeptService;
import com.prd.service.OaPositionService;
import com.prd.service.OaPostService;
import com.prd.util.CommonUtil;
import com.prd.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 部门职务表 前端控制器
 * </p>
 *
 * @author lizhaoyi
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/api/oaPosition")
@Api(tags = "部门职务设置")
public class OaPositionController {

    @Autowired
    private OaPositionService oaPositionService;

    @Autowired
    private OaDeptService oaDeptService;

    @Autowired
    private OaPostService oaPostService;

    @ApiOperation("新增部门职务")
    @PostMapping("add")
    public JsonData add(@RequestBody OaPositionDTO oaPositionDTO){

        OaPositionDO oaPositionDO = oaPositionService.getOne(new LambdaQueryWrapper<OaPositionDO>().eq(OaPositionDO::getPosition, oaPositionDTO.getPosition()));
        if (oaPositionDO != null) {
            return JsonData.buildError("部门职务已存在。");
        }
        OaDeptDO deptDO = oaDeptService.getById(oaPositionDTO.getDeptId());
        if (deptDO == null) {
            return JsonData.buildError("部门不存在");
        }
        OaPostDO oaPostDo = oaPostService.getById(oaPositionDTO.getPostId());
        if (oaPostDo == null) {
            return JsonData.buildError("部门岗位不存在");
        }
        oaPositionDTO.setCreatedTime(LocalDateTime.now());
        oaPositionService.save(CommonUtil.dtoTransfer(oaPositionDTO, OaPositionDO.class));

        return JsonData.buildSuccess(oaPositionDTO);
    }

    @ApiOperation("更新部门职务")
    @PostMapping("update")
    public JsonData update(@RequestBody OaPositionDTO oaPositionDTO){

        oaPositionService.updateById(CommonUtil.dtoTransfer(oaPositionDTO, OaPositionDO.class));

        return JsonData.buildSuccess(oaPositionDTO);
    }


    @ApiOperation("删除部门职务")
    @PostMapping("delete")
    public JsonData delete(@ApiParam("部门职务id") @RequestParam Long id){

        oaPositionService.removeById(id);

        return JsonData.buildSuccess();
    }

    @ApiOperation("查询部门职务列表")
    @PostMapping("selectList")
    public JsonData selectList(){

        return JsonData.buildSuccess(oaPositionService.list());
    }

    @ApiOperation("查询部门职务")
    @PostMapping("selectOne")
    public JsonData selectOne(@ApiParam("部门职务id") @RequestParam Long id){

        return JsonData.buildSuccess(oaPositionService.getById(id));
    }
}

